package org.luxor.common.netty.protocol.message;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.ByteBufUtil;
import org.luxor.common.netty.constant.NettyReasonCode;
import org.luxor.common.netty.protocol.NettyFixedHeader;
import org.luxor.common.netty.protocol.NettyMessageType;
import org.luxor.common.netty.protocol.NettyQoS;
import org.luxor.common.netty.protocol.NettySendReplyMessageVariableHeader;
import org.luxor.commons.core.web.IRestStatus;

/**
 * Netty PubRec Message
 * <p>
 * 服务端接收客户端QOS2质量消息(NettyPublishMessage)后的签收应答消息
 *
 * @author Mr.yan @date 2024/1/31
 */
public class NettyPubRecMessage extends NettyMessage implements ByteBufHolder {

    public NettyPubRecMessage(NettyFixedHeader fixedHeader, Object variableHeader, ByteBuf payload) {
        super(fixedHeader, variableHeader, payload);
    }

    @Override
    public NettySendReplyMessageVariableHeader variableHeader() {
        return (NettySendReplyMessageVariableHeader) super.variableHeader();
    }

    @Override
    public ByteBuf payload() {
        return content();
    }

    @Override
    public ByteBuf content() {
        return ByteBufUtil.ensureAccessible((ByteBuf) super.payload());
    }

    @Override
    public NettyPubRecMessage copy() {
        return replace(content().copy());
    }

    @Override
    public NettyPubRecMessage duplicate() {
        return replace(content().duplicate());
    }

    @Override
    public NettyPubRecMessage retainedDuplicate() {
        return replace(content().retainedDuplicate());
    }

    @Override
    public NettyPubRecMessage replace(ByteBuf content) {
        return new NettyPubRecMessage(fixedHeader(), variableHeader(), content);
    }

    @Override
    public int refCnt() {
        return content().refCnt();
    }

    @Override
    public NettyPubRecMessage retain() {
        content().retain();
        return this;
    }

    @Override
    public NettyPubRecMessage retain(int increment) {
        content().retain(increment);
        return this;
    }

    @Override
    public NettyPubRecMessage touch() {
        content().touch();
        return this;
    }

    @Override
    public NettyPubRecMessage touch(Object hint) {
        content().touch(hint);
        return this;
    }

    @Override
    public boolean release() {
        return content().release();
    }

    @Override
    public boolean release(int decrement) {
        return content().release(decrement);
    }

    public static NettyPubRecMessage from(String messageId) {
        return from(messageId, NettyReasonCode.SUCCESS);
    }

    public static NettyPubRecMessage from(String messageId, IRestStatus restStatus) {
        NettyFixedHeader fixedHeader = new NettyFixedHeader(NettyMessageType.PUB_REC, false, NettyQoS.AT_LEAST_ONCE);
        NettySendReplyMessageVariableHeader variableHeader = NettySendReplyMessageVariableHeader.from(messageId, restStatus);
        return new NettyPubRecMessage(fixedHeader, variableHeader, null);
    }
}
